package com.example.desinger.designPattern;

import com.example.desinger.entity.Students;

// 批量添加学生的过滤器
public class AddStudentsPipeFilter {

    // 管道接口
    public static interface IPipe {
        FileterDecorator getData();

        void setData(FileterDecorator fileterDecorator);
    }

    // 空值过滤器之前的数据
    public static class PipeBase implements IPipe {

        private FileterDecorator fileterDecorator;


        public FileterDecorator getData() {
            return fileterDecorator;
        }

        @Override
        public void setData(FileterDecorator fileterDecorator) {
            this.fileterDecorator = fileterDecorator;
        }

    }

    // 管道数据装饰器
    public static class FileterDecorator {
        private Students students;
        private String error = "";

        public void setStudents(Students students) {
            this.students = students;
        }

        public Students getStudents() {
            return students;
        }

        public void setError(String error) {
            this.error = error;
        }

        public String getError() {
            return error;
        }
    }

    public static interface IFilter {
        void filter(IPipe pipeBefore, IPipe pipeAfter, IPipe pipeError);
    }

    public static class NullFilter implements IFilter {
        @Override
        public void filter(IPipe pipeBefore, IPipe pipeAfter, IPipe pipeError) {
            Students students = pipeBefore.getData().getStudents();
            String error = "";
            if (students.getNickname().equals("")) {
                error += "昵称为空";
            }

            if (students.getAccount().equals("")) {
                error += "账号为空";
            }

            if (students.getPassword().equals("")) {
                error += "密码为空";
            }
            pipeError.setData(pipeBefore.getData());

            if (!error.equals("")) {
                error = "账号：" + students.getAccount() + ", 昵称：" + students.getNickname() + "错误为：" + error;
            }

            pipeError.getData().setError(error);
            pipeAfter.setData(pipeBefore.getData());
        }
    }

    public static class ValidFilter implements IFilter {
        @Override
        public void filter(IPipe pipeBefore, IPipe pipeAfter, IPipe pipeError) {
            Students students = pipeBefore.getData().getStudents();
            String error = pipeError.getData().getError();
            if (students.getNickname().length() > 10) {
                error += "昵称过长";
            }

            if (students.getSex() != 0 && students.getSex() != 1) {
                error += "性别不合法";
            }

            if (students.getAccount().length() > 10) {
                error += "账号过长";
            }

            if (students.getPassword().length() > 10) {
                error += "密码过长";
            }

            pipeError.getData().setError(error);
        }
    }

}
